package com.zhihaihe.file.advice;

import com.zhihaihe.file.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 该注解为统一异常处理的核心
 * @author
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandlerAdvice {

    /**-------- 自定义定异常处理方法 --------**/
    @ExceptionHandler(CommonException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R error(CommonException e) {
        e.printStackTrace();
        log.error("全局异常捕获：" + e);
        return R.fail(e.getCode(),e.getMessage(),null);
    }

}
